const express = require("express");
const router = express.Router();

/* GET home page. */
router.get("/", function (req, res, next) {
  res.send({ data: "成功", code: 200, message: "成功" });
});
// const { check, validationResult } = require("express-validator");
// router.post(
//   "/",
//   [
//     // 验证用户名格式
//     check("username").isLength({ min: 1, max: 20 }),
//     // 验证密码长度至少5个字符
//     check("password").isLength({ min: 5 }),
//     // TODO 按道理来说，登录的验证，应该去数据库里面匹配，根据用户名匹配密码，匹配正确再返回成功
//   ],
//   (req, res) => {
//     console.log(req.body);
//     // 收集验证结果
//     const errors = validationResult(req);
//     console.log(errors.isEmpty(), "errors");
//     if (!errors.isEmpty()) {
//       return res.status(400).json({ errors: errors.array() });
//     } else {
//       res.send({ data: "成功", code: 200, message: "成功" });
//     }
//   }
// );
// username=postman&password=1234  参数会报400
// username=postman&password=123456 参数正常200
/**
 * @swagger
 * /api/getPetList:
 *  get:
 *   tags:
 *     - pet
 *   description: Multiple name values can be provided with comma separated strings
 *   parameters:
 *     - name: name
 *       in: query
 *       description: name values that need to be considered for filter
 *       required: false
 *   responses:
 *     '200':
 *       description: successful operation
 *     '400':
 *       description: Invalid name value
 */
router.post("/", (req, res) => {
  console.log(req.body);
  const { username, password } = req.body;
  if (!username || !password) {
    return res.status(400).json({ error: "用户名和密码必填" });
  }
  try {
    // 查询数据库用户
    // 验证用户是否存在
    // 密码验证
    // 登陆成功响应
    res.send({ data: "登录成功", code: 200, message: "登录成功" });
  } catch (err) {
    console.error("登录错误:", err);
    res.status(500).json({ error: "服务器错误" });
  }
});
module.exports = router;
